<template>
  <div class="">

    <el-form :model="queryParams" label-width="100px" ref="queryRef" :inline="true" v-show="showSearch">

      <el-form-item label="管理行业" prop="noticeTitle">
        <el-input v-model="queryParams.a" placeholder="请输入管理行业" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="用户单位" prop="noticeTitle">
        <el-input v-model="queryParams.b" placeholder="请输入用户单位" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="法人名称" prop="noticeTitle">
        <el-input v-model="queryParams.c" placeholder="请输入法人名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="角色职务" prop="noticeTitle">
        <el-input v-model="queryParams.d" placeholder="请输入角色职务" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="后台功能" prop="noticeTitle">
        <el-input v-model="queryParams.e" placeholder="请输入后台功能" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="后台功能权限" prop="noticeTitle">
        <el-input v-model="queryParams.f" placeholder="请输入后台功能权限" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="行业监管单位" prop="noticeTitle">
        <el-input v-model="queryParams.g" placeholder="请输入行业监管单位" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="信息内容" prop="noticeTitle">
        <el-input v-model="queryParams.h" placeholder="请输入信息内容" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="导出项目" prop="noticeTitle">
        <el-input v-model="queryParams.i" placeholder="请输入信息内容" clearable @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item label="管理区域" prop="noticeTitle">
        <el-input v-model="queryParams.j" placeholder="请输入管理区域" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="单位账号" prop="noticeTitle">
        <el-input v-model="queryParams.k" placeholder="请输入单位账号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="角色名称" prop="noticeTitle">
        <el-input v-model="queryParams.l" placeholder="请输入角色名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="角色职业" prop="noticeTitle">
        <el-input v-model="queryParams.m" placeholder="请输入角色职业" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="app功能" prop="noticeTitle">
        <el-input v-model="queryParams.n" placeholder="请输入app功能" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="app功能权限" prop="noticeTitle">
        <el-input v-model="queryParams.o" placeholder="请输入app功能权限" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="用户初始密码" prop="noticeTitle">
        <el-input v-model="queryParams.p" placeholder="请输入用户初始密码" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="公告内容" prop="noticeTitle">
        <el-input v-model="queryParams.q" placeholder="请输入公告内容" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="日志项目" prop="noticeTitle">
        <el-input v-model="queryParams.r" placeholder="请输入日志项目" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="筛选日期" prop="noticeTitle">
        <el-input v-model="queryParams.ff" placeholder="请输入筛选日期" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="相关查询" prop="noticeTitle">
        <el-input v-model="queryParams.kk" placeholder="请输入相关查询" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <slot></slot>

      <el-form-item style="display: none;">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script setup name="Notice">
import {
  listNotice,
  getNotice,
  delNotice,
  addNotice,
  updateNotice,
} from "@/api/system/notice";

const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type } = proxy.useDict(
  "sys_notice_status",
  "sys_notice_type"
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    currentPage: 1,
    pageSize: 10,
    noticeTitle: undefined,
    createBy: undefined,
    status: undefined,
  },
  rules: {
    noticeTitle: [
      { required: true, message: "公告标题不能为空", trigger: "blur" },
    ],
    noticeType: [
      { required: true, message: "公告类型不能为空", trigger: "change" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询公告列表 */
function getList() {
  loading.value = true;
  listNotice(queryParams.value).then((response) => {
    noticeList.value = [...response.records, ...response.records];
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    noticeId: undefined,
    noticeTitle: undefined,
    noticeType: undefined,
    noticeContent: undefined,
    status: "0",
  };
  proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.noticeId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加公告";
}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const noticeId = row.noticeId || ids.value;
  getNotice(noticeId).then((response) => {
    form.value = response;
    open.value = true;
    title.value = "修改公告";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      if (form.value.noticeId != undefined) {
        updateNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const noticeIds = row.noticeId || ids.value;
  proxy.$modal
    .confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？')
    .then(function () {
      return delNotice(noticeIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

getList();
</script>
